﻿using APIServer.Controllers;
using ParkSaleSDK.Modularity;
using System;
using System.Collections.Generic;
namespace APIServer.ModuleSystem
{

    public abstract class BaseModuleManager<T> where T : class, IModInfo
    {
        protected Dictionary<string, T> dic = new Dictionary<string, T>();

        /// <summary>
        /// 功能允许运行的再确认
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        protected abstract bool Pending(T obj);
        /// <summary>
        /// 查找本地拓展类库文件
        /// </summary>
        /// <param name="path"></param>
        public virtual void Find(string path)
        {
            ModTool.ModHelper mod = new ModTool.ModHelper(path, ModTool.ModFoundOption.BaseClass);
            mod.BaseClass = typeof(T);
            mod = mod.Find();
            foreach (var i in mod.AllType())
            {
                T obj = (T)i.GetConstructor(Type.EmptyTypes).Invoke(null);
                if (Pending(obj))
                    dic.Add(obj.Token(), obj);
            }
        }

        public IEnumerable<T> AllValues()
        {
            return dic.Values;
        }
        public T this[string name]
        {
            get
            {
                try
                {
                    return dic[name];
                }
                catch
                {
                    return null;
                }
            }
        }
    }


    public class DicountModuleManager : BaseModuleManager<IDiscount>
    {
        protected override bool Pending(IDiscount obj)
        {
            return true;
        }
    }

    public class SurveyModuleManager : BaseModuleManager<ISurvey>
    {
        protected override bool Pending(ISurvey obj)
        {
            return true;
        }
    }
    public class MessageModuleManager : BaseModuleManager<IMessage>
    {
        protected override bool Pending(IMessage obj)
        {
            return SUControllers.TokenAuth(obj.SuToken());
        }
    }
}
